/**
 * 
 */
package com.android.weibogo;

import java.util.ArrayList;
import java.util.List;

import com.mime.qweibo.utils.QWeiboType.PageFlag;
import com.mime.qweibo.utils.QWeiboType.WeiboListType;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author xiang.jianhua
 *
 */
public class WeiboCastOrComment extends Activity {
	private long mRootId;
	private String mTokenKey;
	private String mTokenSecret;
	private String mUserName;
	private String mWeiboName;
	private int mWeiboListType;
	private static final String mTag = "WeiboCastOrComment";
	private TecentWeiboModel mWeiboModel;
	private int mRemainTextCount;
	private int mTotalComment;
	private int mTotalCast;
	
	private class CommentListChangeListener implements WeiboModel.WeiboListChangeListener{

		private View progressIndicator;
		/**
		 * @param progressIndicator
		 */
		private CommentListChangeListener(View view) {
			super();
			this.progressIndicator = view;
		}
		@Override
		public void OnWeiboListChange(List<WeiboAdapter> adapterList) {
			if(adapterList != null && adapterList.size() > 0){
				for(WeiboAdapter adapter : adapterList){
					adapter.refresh(true);
				}
			}
			if(progressIndicator != null){
				progressIndicator.setVisibility(View.GONE);
			}
		}
		
	}
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.weibo_cast_comment);
		LinearLayout layout = (LinearLayout) findViewById(R.id.main_layout);
		layout.setBackgroundResource(R.drawable.background);

		Intent intent = getIntent();
		mRootId = Long.valueOf(intent.getStringExtra("rootid"));
		mTokenKey = intent.getStringExtra("tokenkey");
		mTokenSecret = intent.getStringExtra("tokensecret");
		mUserName = intent.getStringExtra("username");
		mWeiboName = intent.getStringExtra("weiboname");
		//default cast type
		mWeiboListType = intent.getIntExtra("weibolisttype", 0); 
		mTotalComment = Integer.valueOf(intent.getStringExtra("totalcomment"));
		mTotalCast = Integer.valueOf(intent.getStringExtra("totalcast"));
		
		mRemainTextCount = 140;
		if (mRootId != -1 && mTokenKey != null && mTokenSecret != null) {
			final LinearLayout loadingLayout = (LinearLayout)findViewById(R.id.loadingLayout);
		   	ListView commentListView = (ListView)findViewById(R.id.comment_list);
			//set the head text
			TextView headText = (TextView)findViewById(R.id.headText);
			if(mWeiboListType == WeiboListType.Comment_list)
				headText.setText(getString(R.string.headtext_comment));
			else if(mWeiboListType == WeiboListType.Cast_list)
				headText.setText(getString(R.string.headtext_cast));
			//set the weibo remain character indicator text
			final TextView remainText = (TextView)findViewById(R.id.remain_text);
			String formatStr = getString(R.string.remain_text);
			remainText.setText(String.format(formatStr, mRemainTextCount));
			//set the total comment text
			TextView totalCommentText = (TextView)findViewById(R.id.total_comment_text);
			formatStr = getString(R.string.total_comment_text);
			totalCommentText.setText(String.format(formatStr, mTotalComment));
			//new comment listview adapter
		   	WeiboCommentListAdapter adapter = new WeiboCommentListAdapter(this, commentListView);
	    	ArrayList<WeiboAdapter> adapterList = new ArrayList<WeiboAdapter>();
	    	adapterList.add(adapter);
	        mWeiboModel = new TecentWeiboModel(this, mTokenKey, mTokenSecret, adapterList);
			//set the listener to model
			mWeiboModel.setWeiboListChangeListener(new CommentListChangeListener(findViewById(R.id.loadingLayout)));
			//set the listener for the listview
		   	commentListView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View view,
						int position, long id) {
					if(position >= mWeiboModel.getWeiboList().size()){
						loadingLayout.setVisibility(View.VISIBLE);
						mWeiboModel.getCommentList(mRootId, PageFlag.PageFlag_Next);
					}
				}
			});
			//set the listener for edit text
			final EditText editText = (EditText)findViewById(R.id.weibo_edit_text);
			editText.addTextChangedListener(new TextWatcher() {
				
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
				}
				
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {
				}
				
				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					int remain_text_count = mRemainTextCount - s.length();
					remainText.setText(String.format(getString(R.string.remain_text), remain_text_count));
				}
			});
			//set the click listener for the back button
			ImageButton backBtn = (ImageButton)findViewById(R.id.back);
			backBtn.setOnTouchListener(new ImageButtonTouchListener());
			backBtn.setOnClickListener(new OnClickListener() {			
				@Override
				public void onClick(View v) {
					finish();
				}
			});
			//set the click listener for comment button
			Button commentBtn = (Button)findViewById(R.id.action_button);
			commentBtn.setOnClickListener(new OnClickListener() {			
				@Override
				public void onClick(View v) {
					if(editText.getText().toString().length() <= 0){
						Builder builder = new AlertDialog.Builder(WeiboCastOrComment.this);
						builder.setCancelable(true);
						builder.setTitle(getString(R.string.system_help_info));
						builder.setMessage(getString(R.string.content_null));
						builder.create().show();
					}else{
						if (mWeiboModel.appraise(mUserName, mRootId, editText
								.getText().toString()))
							Toast.makeText(getApplicationContext(),
									getString(R.string.comment_weibo_success),
									Toast.LENGTH_LONG).show();
						else
							Toast.makeText(getApplicationContext(),
									getString(R.string.comment_weibo_fail),
									Toast.LENGTH_LONG).show();
					}
				}
			});
			//load the comment or cast list
			
			loadingLayout.setVisibility(View.VISIBLE);
			mWeiboModel.getCommentList(mRootId, PageFlag.PageFlag_First);
		} else {
			Log.v(mTag, "invalid parameters, unkown error");
			finish();
		}
	}
	
}
